package com.kaili.common.tenant;

import com.kaili.common.util.CommonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 数据源切换拦截
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 * @author zhengs
 * @version 1.0.0 2018年4月10日 下午3:19:08
 */
@Aspect
public class DataSourceAspect {
//    @Before("execution(* com.kaili.freight.demo.mapper.*.*(..))")
//    @Before("execution(* com.baomidou.mybatisplus.service.impl.*.*(..))")
@Before("execution(* com.kaili.*.controller.*.*.*.*(..)) || execution(* com.kaili.controller.*.*.*.*(..))")
public void setDataSource(JoinPoint point) {
	String methodName = point.getSignature().getName();
	System.out.println("Enter in setDataSource.methodName is " + methodName);
	String type = "write";
	if (methodName.startsWith("select") || methodName.startsWith("get") || methodName.startsWith("query") || methodName.startsWith("search")) {
		type = "read";
	}
	if(methodName.equals("getPageList")){//分页查询要求实时性高,所以走主库
		type = "write";
	}

	String companyId = CommonUtil.getCompanyId();
	DbContextHolder.setDbType(companyId + "_" + type);
}
}
